package edu.chl.dat255.bluebanana.presentation.impl;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import edu.chl.dat255.bluebanana.data.impl.DbConst;
import edu.chl.dat255.bluebanana.data.impl.SQLiteStorage;
import edu.chl.dat255.bluebanana.presentation.IProfileView;
import edu.chl.dat255.bluebanana.utils.DatabaseUtil;
import edu.chl.dat255.bluebanana.utils.ImageAdapter;
import edu.chl.dat255.bluebanana.utils.ObservableAdapter;
import edu.chl.dat255.bluebanana.utils.Observer;

public class ProfileView extends LinearLayout implements IProfileView {
	private String name;
	private ImageView icon;
	private long id;
	private TextView textView;
	private ObservableAdapter obsAda;
	private Button editButton;
	private Button deleteButton;
	private Context context;
	private SQLiteStorage db;

	public ProfileView(final Context context, long id) {
		super(context);
		this.context = context;
		this.id = id;

		db = new SQLiteStorage(context);
        db.open();
		
		textView = new TextView(context);
		textView.setText(DatabaseUtil.getTextValue(id, DbConst.KEY_NAME, context));
		
		db.close();
		
		textView.setTextColor(Color.WHITE);
		editButton = new Button(context);
		editButton.setText("Edit");
		editButton.setOnClickListener(this);
		deleteButton = new Button(context);
		deleteButton.setText("Delete");
		deleteButton.setOnClickListener(this);
		editButton.setGravity(5);
		deleteButton.setGravity(5);
		
		icon = new ImageView(context);
		icon.setImageResource(ImageAdapter.imageIds[DatabaseUtil.getIntValue(id, DbConst.KEY_ICON_PATH, context)]);	
		
		this.addView(icon);
		this.addView(textView);
		this.addView(editButton);
		this.addView(deleteButton);
		
		obsAda = new ObservableAdapter(this);
		this.setBackgroundColor(Color.BLACK);
		
		this.setOnClickListener(this);
	}
	
	@Override
	protected void onDraw (Canvas canvas) {
		super.onDraw(canvas);
		icon.setImageResource(ImageAdapter.imageIds[DatabaseUtil.getIntValue(id, DbConst.KEY_ICON_PATH, context)]);
		setName(DatabaseUtil.getTextValue(this.getRowId(), DbConst.KEY_NAME, context));
	}
	
	@Override
	/**
	 * @inheritDoc
	 */
	public String getName() {
		return name;
	}
	
	@Override
	/**
	 * @inheritDoc
	 */
	public ImageView getIcon() {
		return icon;
	}
	
	@Override
	/**
	 * @inheritDoc
	 */
	public void setName(String newName) {
		textView.setText(newName);
	}
	
	@Override
	/**
	 * @inheritDoc
	 */
	public void setIcon(ImageView newIcon) {
		icon.setImageResource(newIcon.getId());
	}

	@Override
	public boolean addObserver(Observer o) {
		return obsAda.addObserver(o);
	}

	@Override
	public boolean deleteObserver(Observer o) {
		return obsAda.deleteObserver(o);
	}

	@Override
	public void deleteObservers() {
		obsAda.deleteObservers();	
	}

	@Override
	public boolean hasChanged() {
		return obsAda.hasChanged();
	}

	@Override
	public void notifyObservers(Object state) {
		obsAda.setChanged();
		obsAda.notifyObservers(state);
	}

	@Override
	public void onClick(View v) {
		if (v.equals(this)) {
			obsAda.setChanged();
			notifyObservers(this.getRowId());
		} else if (v.equals(deleteButton)) {
			DatabaseUtil.delete(this.getRowId(), context);

	        setVisibility(GONE); //Hides the ProfileView but it still exists
		} else if (v.equals(editButton)) {
			Intent intent = new Intent(getContext(), ProfileSettings.class);
			intent.putExtra("Profile", this.getRowId());
			getContext().startActivity(intent);
		}
	}

	@Override
	public long getRowId() {
		return id;
	}
}
